#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QGraphicsDropShadowEffect>

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    m_gitPageBtn = new ClickableLabel("git", this);
    m_gitPageBtn->setShape(ClickableLabel::Shape::Rectangle);
    m_gitPageBtn->setMargins(0, 0, 0, 0);

    m_homePageBtn = new ClickableLabel("首页", this);
    m_homePageBtn->setMode(ClickableLabel::Mode::ExclusiveSelectable);
    m_homePageBtn->setShape(ClickableLabel::Shape::Rectangle);
    m_homePageBtn->setMinimumHeight(45);
    m_homePageBtn->setMargins(10, 8, 10, 8);
    m_homePageBtn->setDefaultPixmap(QPixmap(":/icons/navigation-home-default.svg"));
    m_homePageBtn->setHoveredPixmap(QPixmap(":/icons/navigation-home-hovered.svg"));
    m_homePageBtn->setSelectedPixmap(QPixmap(":/icons/navigation-home-selected.svg"));
    m_homePageBtn->setPressedColor("#dff9e2");
    m_homePageBtn->setSelectedColor("#dff9e2");
    m_homePageBtn->setContentAlignment(ClickableLabel::ContentAlignment::Left);

    m_addJobPosBtn = new ClickableLabel("添加岗位", this);
    m_addJobPosBtn->setMode(ClickableLabel::Mode::ExclusiveSelectable);
    m_addJobPosBtn->setShape(ClickableLabel::Shape::Rectangle);
    m_addJobPosBtn->setMinimumHeight(45);
    m_addJobPosBtn->setMargins(10, 8, 10, 8);
    m_addJobPosBtn->setDefaultPixmap(QPixmap(":/icons/navigation-add-default.svg"));
    m_addJobPosBtn->setHoveredPixmap(QPixmap(":/icons/navigation-add-hovered.svg"));
    m_addJobPosBtn->setSelectedPixmap(QPixmap(":/icons/navigation-add-selected.svg"));
    m_addJobPosBtn->setPressedColor("#dff9e2");
    m_addJobPosBtn->setSelectedColor("#dff9e2");
    m_addJobPosBtn->setContentAlignment(ClickableLabel::ContentAlignment::Left);

    m_manageJobPosBtn = new ClickableLabel("岗位管理", this);
    m_manageJobPosBtn->setMode(ClickableLabel::Mode::ExclusiveSelectable);
    m_manageJobPosBtn->setShape(ClickableLabel::Shape::Rectangle);
    m_manageJobPosBtn->setMinimumHeight(45);
    m_manageJobPosBtn->setMargins(10, 8, 10, 8);
    m_manageJobPosBtn->setDefaultPixmap(QPixmap(":/icons/navigation-manage-default.svg"));
    m_manageJobPosBtn->setHoveredPixmap(QPixmap(":/icons/navigation-manage-hovered.svg"));
    m_manageJobPosBtn->setSelectedPixmap(QPixmap(":/icons/navigation-manage-selected.svg"));
    m_manageJobPosBtn->setPressedColor("#dff9e2");
    m_manageJobPosBtn->setSelectedColor("#dff9e2");
    m_manageJobPosBtn->setContentAlignment(ClickableLabel::ContentAlignment::Left);

    m_interviewReviewBtn = new ClickableLabel("面试复盘", this);
    m_interviewReviewBtn->setMode(ClickableLabel::Mode::ExclusiveSelectable);
    m_interviewReviewBtn->setShape(ClickableLabel::Shape::Rectangle);
    m_interviewReviewBtn->setMinimumHeight(45);
    m_interviewReviewBtn->setMargins(10, 8, 10, 8);
    m_interviewReviewBtn->setDefaultPixmap(QPixmap(":/icons/navigation-review-default.svg"));
    m_interviewReviewBtn->setHoveredPixmap(QPixmap(":/icons/navigation-review-hovered.svg"));
    m_interviewReviewBtn->setSelectedPixmap(QPixmap(":/icons/navigation-review-selected.svg"));
    m_interviewReviewBtn->setPressedColor("#dff9e2");
    m_interviewReviewBtn->setSelectedColor("#dff9e2");
    m_interviewReviewBtn->setContentAlignment(ClickableLabel::ContentAlignment::Left);

    m_profileBtn = new ClickableLabel(this);
    m_profileBtn->setShape(ClickableLabel::Shape::Circle);
    m_profileBtn->setRadius(30);
    m_profileBtn->setFixedSize(30, 30);
    m_profileBtn->setMargins(3, 3, 3, 3);

    m_profileTip = new ClickableLabel(this);
    m_profileTip->setShape(ClickableLabel::Shape::Rectangle);
    m_profileBtn->setMargins(0, 0, 0, 0);

    initUi();
    initSlots();

    // 设置默认选中首页
    m_homePageBtn->setSelected(true);
}

MainWindow::~MainWindow()
{
    delete ui;
}

// void MainWindow::resizeEvent(QResizeEvent *event)
// {
//     // 先调用父类的resizeEvent，确保正常的窗口大小调整功能
//     QMainWindow::resizeEvent(event);

//     // 获取窗口当前大小
//     QSize newSize = event->size();

//     // 打印窗口大小信息到调试输出
//     qDebug() << "窗口大小改变: " << newSize.width() << "x" << newSize.height();
// }

void MainWindow::initUi()
{
    ui->GitLayout->addWidget(m_gitPageBtn);
    ui->FuncsLayout->addWidget(m_homePageBtn);
    ui->FuncsLayout->addWidget(m_addJobPosBtn);
    ui->FuncsLayout->addWidget(m_manageJobPosBtn);
    ui->FuncsLayout->addWidget(m_interviewReviewBtn);
    ui->ProfileLayout->addWidget(m_profileBtn);
    ui->ProfileLayout->addWidget(m_profileTip);

    QGraphicsDropShadowEffect *shadowEffect = new QGraphicsDropShadowEffect(this);
    // 设置阴影属性
    shadowEffect->setBlurRadius(20);  // 阴影模糊半径
    shadowEffect->setColor(QColor(0, 0, 0, 35));  // 阴影颜色（带透明度）
    shadowEffect->setOffset(0, 3);  // 阴影偏移量（x, y）
    // 应用阴影到topWidget
    ui->topWidget->setGraphicsEffect(shadowEffect);
}

void MainWindow::initSlots()
{
    // 连接四个导航项的选中状态变化以及页面切换信号
    connect(m_homePageBtn, &ClickableLabel::clicked, this, [this]() {
        setNavItemSelected(m_homePageBtn);
        // if(!m_homePage){
        //     m_homePage = new HomePage(this);
        // }
        // if(ui->stackedPage->indexOf(m_homePage) == -1){
        //     ui->stackedPage->insertWidget(0, m_homePage);
        // }
        // ui->stackedPage->setCurrentIndex(0);
    });
    connect(m_addJobPosBtn, &ClickableLabel::clicked, this, [this]() {
        setNavItemSelected(m_addJobPosBtn);
        if(!m_addJobPosPage){
            m_addJobPosPage = new AddJobPosPage(this);
        }
        if(ui->stackedPage->indexOf(m_addJobPosPage) == -1){
            ui->stackedPage->insertWidget(1, m_addJobPosPage);
        }
        ui->stackedPage->setCurrentIndex(1);
    });
    connect(m_manageJobPosBtn, &ClickableLabel::clicked, this, [this]() {
        setNavItemSelected(m_manageJobPosBtn);
    });
    connect(m_interviewReviewBtn, &ClickableLabel::clicked, this, [this]() {
        setNavItemSelected(m_interviewReviewBtn);
    });

    // 其他按钮的信号连接
    connect(m_gitPageBtn, &ClickableLabel::clicked, this, &MainWindow::onGitPageClicked);
    connect(m_profileBtn, &ClickableLabel::clicked, this, &MainWindow::onProfileClicked);
    connect(m_profileTip, &ClickableLabel::clicked, this, &MainWindow::onProfileClicked);
}

// 导航项选中处理函数
void MainWindow::setNavItemSelected(ClickableLabel* target)
{
    // 如果点击的是已经选中的项，不做任何操作
    if (target->isSelected()) {
        return;
    }

    // 取消所有导航项的选中状态
    m_homePageBtn->setSelected(false);
    m_addJobPosBtn->setSelected(false);
    m_manageJobPosBtn->setSelected(false);
    m_interviewReviewBtn->setSelected(false);

    // 选中目标项
    target->setSelected(true);
}

void MainWindow::onGitPageClicked()
{
    // Git页面点击处理
}

void MainWindow::onProfileClicked()
{
    // 个人资料点击处理
}
